<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_News extends Controller_Admin {

    public function action_add() {
        //
        
        $this->_active_menu_left = 'newsadd';
        $breadcrumbs = array(
            "News" => "",
            "Add" => "",
        );
        $this->add_js(array(
            'fuelux/fuelux.wizard.min',
            'jquery.validate.min',
            'jquery-ui-1.10.3.custom.min',
            'jquery.ui.touch-punch.min',
            'markdown/markdown.min',
            'markdown/bootstrap-markdown.min',
            'jquery.hotkeys.min',
            'tinymce/tinymce.min',
            'additional-methods.min',
            'bootbox.min',
            'jquery.maskedinput.min',
            'select2.min',
            'ajaxupload',
            'date-time/bootstrap-datepicker.min',
            'date-time/bootstrap-timepicker.min',
            'include/news/add',
            'ace-elements.min',
            'ace.min',
        ));
        $this->add_css(array('select2', 'upload', 'datepicker'));

        $en_news = new Domain_Entities_New();
        $s_news_la = new Model_Service_New_Language();
        $en_news_la = array();
        $en_news_la[1] = new Domain_Entities_New_Language();
        $en_news_la[2] = new Domain_Entities_New_Language();

        if ($_POST) {
            header('Content-type: application/json');
            $a_post = $this->request->post();
            $en_news->set_array_to_object($a_post);
            $title = Arr::get($_POST, 'title');
            $short_descriptionx = Arr::get($_POST, 'short_descriptionx');
            $content = Arr::get($_POST, 'description');
            $file_pdf = Arr::get($_POST, 'file_pdf');
            if (trim($title[1]) == "") {
                echo json_encode(array('error' => 1, 'id' => 'title1', 'msg' => 'Please input title of Vietnamese.'));
                die;
            }

            if (trim($title[2]) == "") {
                echo json_encode(array('error' => 1, 'id' => 'title2', 'msg' => 'Please input title of English.'));
                die;
            }

            if (trim($short_descriptionx[1]) == "") {
                echo json_encode(array('error' => 1, 'id' => 'short_description1', 'msg' => 'Please input Short description of Vietnamese.'));
                die;
            }

            if (trim($short_descriptionx[2]) == "") {
                echo json_encode(array('error' => 1, 'id' => 'short_description2', 'msg' => 'Please input Short description of English.'));
                die;
            }

            $en_news->setstartdate(Controller_Helper_Format::format_date($en_news->getstartdate(), 'm/d/Y', 'Y-m-d'));
            $en_news->settitle($title[1]);
            $en_news->setType('NEWS');
            $en_news->setCreated(date('Y-m-d H:i:s', strtotime('now')));
            $en_news->setModified(date('Y-m-d H:i:s', strtotime('now')));
            $s_news = new Model_Service_New();
            $en_news = $s_news->insert($en_news)->get_data();
            $file = array();
            if (trim($en_news->getImage()) != "") {
                $file[] = $en_news->getImage();
            }

            if ($en_news->getId() > 0) {
                for ($i = 1; $i <= 2; $i++) {
                    $en_news_la[$i]->setTitle($title[$i]);
                    $en_news_la[$i]->setContent($content[$i]);
                    $en_news_la[$i]->setShort_Content($short_descriptionx[$i]);
                    $en_news_la[$i]->setCreated(date('Y-m-d H:i:s', strtotime('now')));
                    $en_news_la[$i]->setLanguage_id($i);
                    $en_news_la[$i]->setfile_pdf($file_pdf[$i]);
                    $en_news_la[$i]->setNews_id($en_news->getId());

                    $s_news_la->insert($en_news_la[$i]);
                    if (trim($en_news_la[$i]->getfile_pdf()) != "") {
                        $file[] = $en_news_la[$i]->getfile_pdf();
                    }
                }
            }
            $o_message = Controller_Helper_Message::get_instance();
            $o_message->set_success('add', $this->getLanguage() . '/admin/user/success');
            if ($en_news->getID() > 0 && !empty($file)) {
                $file = implode(",", $file);
                $url_move = $this->getUrl_base_media_location() . "/move_file.php";
                $request = Request::factory($url_move)
                        ->method('POST')
                        ->post('fromfolder', Controller_Helper_Constant::FOLDER_TEMP)
                        ->post('tofolder', Controller_Helper_Constant::FOLDER_NEWS)
                        ->post('filename', $file);
                $response = $request->execute();
            }
            // send notification
            if ($en_news->getActive() == 1) {
                Controller_Helper_Notification::sent($en_news->getID(), 'google', 'apple');
            }
            echo json_encode(array('error' => 0));
            die;
        }

        $this->template->content = View::factory('admin/news/add')
                ->set('breadcrumbs', $breadcrumbs)
                ->set('en_news_la', $en_news_la)
                ->set('en_news', $en_news);
    }

    public function action_list() {
        $this->_active_menu_left = 'newslist';
        $breadcrumbs = array(
            "News" => "",
            "List" => "",
        );
        $this->add_js(array(
            
            'date-time/bootstrap-datepicker.min',
            'date-time/bootstrap-timepicker.min',
            
        ));
        $this->add_css(array('datepicker'));
        $this->add_js(array(
            'ace-elements.min',
            'ace.min',
        ));

        $search = Arr::get($_GET, 'textsearch');
        $active = Arr::get($_GET, 'active', 1);
        $a_pagination = Kohana::$config->load("pagination");
        $page = Arr::get($_GET, 'page') == "" ? 1 : Arr::get($_GET, 'page');
        $items_per_page = Arr::get($_GET, 'size') != "" ? Arr::get($_GET, 'size') : Arr::path($a_pagination, 'admin.items_per_page');
        $to = $page * $items_per_page;
        $offset = $to - $items_per_page;
        $a_pagination->admin["items_per_page"] = $items_per_page;
        $a_pagination->admin["view"] = 'pagination/menu';
        $a_pagination->admin["menuid"] = '';

        $c = Arr::get($_REQUEST, 'category');
        $s = Arr::get($_REQUEST, 'sub_category');
        
        $date = Arr::get($_REQUEST, 'date');
        $date = Controller_Helper_Format::format_date($date, 'm/d/Y', 'Y-m-d');
        $s_news = new Model_Service_New();
        $a_news = $s_news->getall_admin('NEWS', $search, $active, $c, $s, 1, $offset, $items_per_page,false,$date);
        $n_count_all = $s_news->getall_admin('NEWS', $search, $active, $c, $s, 1, null, null, true,$date);

        Arr::set_path($a_pagination, 'admin.total_items', $n_count_all);
        $pagination = new Pagination(Arr::get($a_pagination, 'admin'));


        $this->template->content = View::factory('admin/news/list')
                ->set('breadcrumbs', $breadcrumbs)
                ->set('a_news', $a_news)
                ->set('pagination', $pagination);
    }

    public function action_deleted() {
        $id = Arr::get($_REQUEST, 'id');
        $s_news = new Model_Service_New();
        $en_news = $s_news->load($id)->get_data();
        $file = array();
        if (trim($en_news->getImage()) != "") {
            $file[] = $en_news->getImage();
        }

        $s_news_la = new Model_Service_New_Language();
        $a_news_la = $s_news_la->getitem($id);
        if (!empty($a_news_la)) {
            foreach ($a_news_la as $obj) {
                $en = $obj->get_data();
                if (trim($en->getfile_pdf()) != "") {
                    $file[] = $en->getfile_pdf();
                }
                $s_news_la->delete($en->getId());
            }
        }
        if (!empty($file)) {
            $file = implode(",", $file);
            $url_delete = $this->getUrl_base_media_location() . "/delete_file.php";
            $request = Request::factory($url_delete)
                    ->method('POST')
                    ->post('fromfolder', Controller_Helper_Constant::FOLDER_NEWS)
                    ->post('filename', $file);
            $response = $request->execute();
        }
        $s_news->delete($id);

        $s_views = new Model_Service_View();
        $s_views->delete_by_ids($id);

        $s_bookmark = new Model_Service_Bookmark();
        $s_bookmark->delete_by_ids($id);

        $s_like = new Model_Service_Like();
        $s_like->delete_by_ids($id);


        $s_attend = new Model_Service_Attend();
        $s_attend->delete_by_ids($id);

        $o_message = Controller_Helper_Message::get_instance();
        $o_message->set_success('delete', $this->getLanguage() . '/admin/user/success');
        die;
    }

    public function action_edit() {
        $this->_active_menu_left = 'newsedit';
        $breadcrumbs = array(
            "News" => "",
            "Edit" => "",
        );
        $this->add_js(array(
            'fuelux/fuelux.wizard.min',
            'jquery.validate.min',
            'jquery-ui-1.10.3.custom.min',
            'jquery.ui.touch-punch.min',
            'markdown/markdown.min',
            'markdown/bootstrap-markdown.min',
            'jquery.hotkeys.min',
            'tinymce/tinymce.min',
            'additional-methods.min',
            'bootbox.min',
            'jquery.maskedinput.min',
            'select2.min',
            'ajaxupload',
            'date-time/bootstrap-datepicker.min',
            'date-time/bootstrap-timepicker.min',
            'include/news/edit',
            'ace-elements.min',
            'ace.min',
        ));
        $this->add_css(array('select2', 'upload', 'datepicker'));
        $id = Arr::get($_REQUEST, 'id');
        $s_news = new Model_Service_New();
        $en_news = $s_news->load($id)->get_data();
        $s_news_la = new Model_Service_New_Language();

        $en_news_la = array();
        $en_news_la[1] = $s_news_la->get($id, 1)->get_data();
        $en_news_la[2] = $s_news_la->get($id, 2)->get_data();

        $f1 = $en_news_la[1]->getfile_pdf();
        $f2 = $en_news_la[2]->getfile_pdf();
        $f = $en_news->getImage();
        $active = $en_news->getActive();
        if ($_POST) {
            header('Content-type: application/json');
            $a_post = $this->request->post();
            $en_news->set_array_to_object($a_post);
            $title = Arr::get($_POST, 'title');
            $short_descriptionx = Arr::get($_POST, 'short_descriptionx');
            $content = Arr::get($_POST, 'description');
            $file_pdf = Arr::get($_POST, 'file_pdf');
            if (trim($title[1]) == "") {
                echo json_encode(array('error' => 1, 'id' => 'title1', 'msg' => 'Please input title of Vietnamese.'));
                die;
            }

            if (trim($title[2]) == "") {
                echo json_encode(array('error' => 1, 'id' => 'title2', 'msg' => 'Please input title of English.'));
                die;
            }

            if (trim($short_descriptionx[1]) == "") {
                echo json_encode(array('error' => 1, 'id' => 'short_description1', 'msg' => 'Please input Short description of Vietnamese.'));
                die;
            }

            if (trim($short_descriptionx[2]) == "") {
                echo json_encode(array('error' => 1, 'id' => 'short_description2', 'msg' => 'Please input Short description of English.'));
                die;
            }

            $en_news->setstartdate(Controller_Helper_Format::format_date($en_news->getstartdate(), 'm/d/Y', 'Y-m-d'));
            $en_news->settitle($title[1]);
            $en_news->setType('NEWS');
            $en_news->setModified(date('Y-m-d H:i:s', strtotime('now')));

            $en_news = $s_news->update($en_news)->get_data();
            $file = array();
            if (trim($en_news->getImage()) != "") {
                $file[] = $en_news->getImage();
            }
            $file_delete = array();
            if ($en_news->getId() > 0) {
                for ($i = 1; $i <= 2; $i++) {
                    $en_news_la[$i]->setTitle($title[$i]);
                    $en_news_la[$i]->setContent($content[$i]);
                    $en_news_la[$i]->setShort_Content($short_descriptionx[$i]);
                    $en_news_la[$i]->setModified(date('Y-m-d H:i:s', strtotime('now')));
                    $en_news_la[$i]->setLanguage_id($i);
                    $en_news_la[$i]->setfile_pdf($file_pdf[$i]);
                    $en_news_la[$i]->setNews_id($en_news->getId());
                    if ($en_news_la[$i]->getId() <= 0) {
                        $s_news_la->insert($en_news_la[$i]);
                    } else {
                        $s_news_la->update($en_news_la[$i]);
                    }
                    if (trim($en_news_la[$i]->getfile_pdf()) != "") {
                        if (($i == 1 && $f1 != $en_news_la[$i]->getfile_pdf()) ||
                                ($i == 2 && $f2 != $en_news_la[$i]->getfile_pdf()))
                            $file[] = $en_news_la[$i]->getfile_pdf();
                    }

                    if ($i == 1 && $f1 != $en_news_la[$i]->getfile_pdf() && !empty($f1)) {
                        $file_delete[] = $f1;
                    }
                    if ($i == 2 && $f2 != $en_news_la[$i]->getfile_pdf() && !empty($f2)) {
                        $file_delete[] = $f2;
                    }
                }
            }
            $o_message = Controller_Helper_Message::get_instance();
            $o_message->set_success('edit', $this->getLanguage() . '/admin/user/success');
            if ($f != $en_news->getImage()) {
                $file[] = $en_news->getImage();
            }
            if ($f != $en_news->getImage() && !empty($f)) {
                $file_delete[] = $f;
            }
            if ($en_news->getID() > 0 && !empty($file)) {
                $file = implode(",", $file);
                $url_move = $this->getUrl_base_media_location() . "/move_file.php";
                $request = Request::factory($url_move)
                        ->method('POST')
                        ->post('fromfolder', Controller_Helper_Constant::FOLDER_TEMP)
                        ->post('tofolder', Controller_Helper_Constant::FOLDER_NEWS)
                        ->post('filename', $file);
                $response = $request->execute();
            }
            if (!empty($file_delete)) {
                $file_delete = implode(",", $file_delete);
                $url_delete = $this->getUrl_base_media_location() . "/delete_file.php";
                $request = Request::factory($url_delete)
                        ->method('POST')
                        ->post('fromfolder', Controller_Helper_Constant::FOLDER_NEWS)
                        ->post('filename', $file_delete);
                $response = $request->execute();
            }
            if ($en_news->getActive() == 1 && $active == 0) {
                Controller_Helper_Notification::sent($en_news->getID(), 'google', 'apple');
            }
            echo json_encode(array('error' => 0));
            die;
        }

        $this->template->content = View::factory('admin/news/edit')
                ->set('breadcrumbs', $breadcrumbs)
                ->set('en_news_la', $en_news_la)
                ->set('en_news', $en_news);
    }

    public function action_push() {
       
        Controller_Helper_Notification::sent(51);
        echo "ss";
        die;
    }

}

?>
